package com.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;

import com.pccw.hessian.support.cache.CacheHandler;
import com.pccw.hessian.support.cache.CachePolicy;

public class CRMCacheHandler implements CacheHandler{
	
	private HashMap<String,Object> cache=new HashMap<String, Object>();
	
	@Override
	public boolean ifReturnFromCahce(Method method, Object[] args) {
		try {
			System.out.println("生成的缓存key:"+method.getAnnotation(CachePolicy.class).keyGenerater().newInstance().generatKey(method, args));
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		if("getContacts".equals(method.getName()) && cache.containsKey("getContacts")){
			return true;
		}
		return false;
	}

	@Override
	public Object returnFromCahce(Method method, Object[] args) {
		return cache.get(method.getName());
	}

	@Override
	public void onExecuteSuccess(Method method, Object[] args, Object result) {
		if("getContacts".equals(method.getName())){
			cache.put("getContacts", result);
		}
	}

}
